package com.lemon.springcloud.core.component.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import java.io.IOException;
import java.util.Map;

/**
 * Class Name: AbstractQueue
 * Create Date: 6/27/18 5:16 PM
 * Creator: lemon
 * Version: v1.0
 * Updater:
 * Date Time:
 * Description:
 */
public final class MQProp {

    private String routingKey = null;

    private String bindingKey = null;

    private Exchange exchange = null;

    private boolean durable = true;

    private boolean exclusive = false;

    private boolean autoDelete = false;

    private Map<String, Object> args = null;

    private AMQP.BasicProperties basicProperties = null;

    private Channel channel = null;

    private boolean doNotCreate = false;

    void channel(Channel channel) {
        this.channel = channel;
    }

    void buildAndBindQueue() throws IOException {
        if (!doNotCreate) {
            if (this.exchange == null || this.exchange.name == null || this.exchange.name.length() == 0) {
                channel.queueDeclare(routingKey, durable, exclusive, autoDelete, args);
                return;
            }
            channel.exchangeDeclare(exchange.name, exchange.topic, exchange.durable,
                    exchange.autoDelete, exchange.args);
            channel.queueDeclare(routingKey, durable, exclusive, autoDelete, args);
            channel.queueBind(routingKey, exchange.name, bindingKey == null ? routingKey : bindingKey);
        }
    }

    public static MQProp buildQueue(String routingKey, String bindingKey, Exchange exchange) {
        return new MQProp(routingKey, bindingKey, exchange);
    }

    public MQProp(String routingKey) {
        this.routingKey = routingKey;
    }

    public MQProp(String routingKey, String bindingKey, Exchange exchange) {
        this.routingKey = routingKey;
        this.bindingKey = bindingKey;
        this.exchange = exchange;
    }

    public MQProp(String routingKey, String bindingKey, Exchange exchange, boolean doNotCreate) {
        this.routingKey = routingKey;
        this.bindingKey = bindingKey;
        this.exchange = exchange;
        this.doNotCreate = doNotCreate;
    }

    public String getRoutingKey() {
        return routingKey;
    }

    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }

    public Exchange getExchange() {
        return exchange == null ? new Exchange("") : exchange;
    }

    public void setExchange(Exchange exchange) {
        this.exchange = exchange;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getBindingKey() {
        return bindingKey;
    }

    public void setBindingKey(String bindingKey) {
        this.bindingKey = bindingKey;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public Map<String, Object> getArgs() {
        return args;
    }

    public void setArgs(Map<String, Object> args) {
        this.args = args;
    }

    public AMQP.BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(AMQP.BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public static class Exchange {

        public BuiltinExchangeType topic = BuiltinExchangeType.TOPIC;
        String name = null;
        boolean durable = false;
        boolean exclusive = false;
        boolean autoDelete = false;
        Map<String, Object> args = null;

        public Exchange() {
        }

        public Exchange(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }
}
